from schema import Service
import uuid
from store import flows

intents=dict()
every_slice_intents_num = dict()
every_service_intents_num = dict()

slice_types={
    0:"时延敏感",
    1:"吞吐敏感",
    2:"时延吞吐敏感",
    3:"时延丢包敏感"
}

for slice_name in slice_types.values():
    every_slice_intents_num[slice_name] = 0

service2slice_type={
    "movie":1,
    "music":0,
    "live_video":2,
    "live_game":3,
    "voice_call":0,
    "email_web":2
}

service2dst_ip_port={
    "live_video":("10.0.1.4",8080),
    "movie":("10.0.1.5",80),
    "music":("10.0.1.1",80),
    "voice_call":("10.0.1.4",8080),
    "email_web":("10.0.1.4",8080),
    "live_game":("10.0.1.4",8080)
}

for service in service2slice_type.keys():
    every_service_intents_num[service] = 0

#throughput(mbps), delay, loss
slots_key_qos={
    "movie":{
        #           up-throughput down-throughput  delay  loss
        "360p":    (    8e-3,          1,          20,  0.4e-2),
        "720p":    (    8e-3,          4,          20,  0.2e-2),
        "1080p":   (    8e-3,          18,         20,  0.1e-2),
        "4k":      (    8e-3,          45,         20,  0.05e-2),
        "default": (    8e-3,          4,          20,  0.2e-2)
    },
    "live_video":{
        #           up-throughput down-throughput  delay  loss
        "360p":    (    8e-3,             1,       50,  0.5e-2),
        "720p":    (    8e-3,             4,       50,  0.5e-2),
        "1080p":   (    8e-3,             18,      50,  0.5e-2),
        "4k":      (    8e-3,             45,      50,  0.5e-2),
        "default": (    8e-3,             4,       50,  0.5e-2)
    },
    "music":{
        "default":(0.032, 2,  20, 0.05e-2)
    },
    "voice_call":{
        "default":(0.064, 0.064, 150, 0.5e-2)
    },
    "email_web":{
        "default":(0.02, 10, 20, 0.08e-2)
    },
    "live_game":{
        "default":(1, 1, 50,0.1e-2)
    }
}


background_slice2service = {
    0:"voice_call",
    1:"movie",
    2:"live_video",
    3:"live_game"
}

def add_intent(intent, start_datetime, end_datetime, src_ip, service_type, qos_value:str):
    id = str(uuid.uuid4())
    (dst_ip, dst_port) = service2dst_ip_port[service_type]
    slice_type = service2slice_type[service_type]
    (up_throughput_mbps, down_throughput_mbps, delay, loss)=slots_key_qos[service_type][qos_value.lower()]
    service = Service(id, ' '.join(intent), start_datetime, end_datetime, src_ip, dst_ip, dst_port, up_throughput_mbps, down_throughput_mbps,  loss, delay, service_type, slice_type, slice_types[slice_type])
    intents[id] = service

    if every_service_intents_num.get(service_type) is not None:
        every_service_intents_num[service_type] += 1
    else:
        every_service_intents_num[service_type] = 1
    
    slice_name = slice_types[slice_type]
    every_slice_intents_num[slice_name] += 1

    flows.add_flow(service)
    

def add_background_intent(start_datetime, end_datetime, src_ip, dst_ip, dst_port, up_throughput, down_throughput, loss, delay, slice_type):
    id = str(uuid.uuid4())
    service_type = background_slice2service[slice_type]
    service = Service(id, "Background flow.", start_datetime, end_datetime, src_ip, dst_ip, dst_port, up_throughput, down_throughput, loss, delay, service_type, slice_type, slice_types[slice_type])
    intents[id]=service

    if every_service_intents_num.get(service_type) is not None:
        every_service_intents_num[service_type] += 1
    else:
        every_service_intents_num[service_type] = 1
    
    slice_name = slice_types[slice_type]
    every_slice_intents_num[slice_name] += 1

    flows.add_flow(service)


def get_every_slice_intents():
    return every_slice_intents_num

def get_every_service_intents():
    return every_service_intents_num


def get_intents_json():
    return [intent.__dict__ for intent in intents.values()]

def del_intent(id):
    if intents.get(id) is not None:
        intent = intents[id]
        if every_slice_intents_num[intent.slice_type] != 0:
            every_slice_intents_num[intent.slice_type] -= 1
        if every_service_intents_num[intent.service_type] !=0:
            every_service_intents_num[intent.service_type] -= 1
        del intents[id]
        return 0
    else: 
        return -1